package com.mdd.admin.controller;

import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IApplicantService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ApplicantCreateValidate;
import com.mdd.admin.validate.ApplicantUpdateValidate;
import com.mdd.admin.validate.ApplicantSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ApplicantListedVo;
import com.mdd.admin.vo.ApplicantDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/applicant")
@Api(tags = "申请人列管理")
public class ApplicantController {

    @Resource
    IApplicantService iApplicantService;

    @GetMapping("/list")
    @ApiOperation(value="申请人列列表")
    public AjaxResult<PageResult<ApplicantListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated ApplicantSearchValidate searchValidate) {
        PageResult<ApplicantListedVo> list = iApplicantService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/all")
    @ApiOperation(value="申请人列列表")
    public AjaxResult<List<ApplicantListedVo>> all(@Validated ApplicantSearchValidate searchValidate) {
        searchValidate.setUserId(LikeAdminThreadLocal.getAdminId());
        List<ApplicantListedVo> list = iApplicantService.all(searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="申请人列详情")
    public AjaxResult<ApplicantDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        ApplicantDetailVo detail = iApplicantService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "申请人列新增")
    @PostMapping("/add")
    @ApiOperation(value="申请人列新增")
    public AjaxResult<Object> add(@Validated @RequestBody ApplicantCreateValidate createValidate) {
        iApplicantService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "申请人列编辑")
    @PostMapping("/edit")
    @ApiOperation(value="申请人列编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody ApplicantUpdateValidate updateValidate) {
        iApplicantService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "申请人列删除")
    @PostMapping("/del")
    @ApiOperation(value="申请人列删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iApplicantService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
